/*
 * SensorTask.java
 *
 * Created on February 25, 2008, 6:51 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.sunspotworld.sensortasks;

import java.util.Random;
import java.util.TimerTask;
import org.sunspotworld.hardware.LEDManager;

/**
 * 
 * @author me
 */
public abstract class SensorTask extends TimerTask {
    protected long sampleInterval;
    protected boolean repeating=false;
    private static Random rand = new Random(System.currentTimeMillis());
    protected long id = rand.nextLong();
    
    public long getID() {
        return id;
    }
    
    public void setRepeat(boolean on) {
        repeating = on;
    }
    
    public void run() {
        TimeKeeper.getInstance().registerCurrentTask(this);
        runTask();
        if(repeating) {
            TimeKeeper.getInstance().schedule(this.copy());
        }
        TimeKeeper.getInstance().registerCurrentTask(null);
        LEDManager.getInstance().clearLEDs();
    }
    
    public long getSampleInterval() {
        return sampleInterval;
    }
    
    public int hashCode() {
        int hash = 7;
        hash = 41 * hash + (int) (this.id ^ (this.id >>> 32));
        return hash;
    }
    
    public boolean equals(Object o) {
        try {
            return ((SensorTask)o).getID() == this.id;
        } catch (Exception ex) {
            return false;
        }
    }
    
    protected abstract SensorTask copy();
    
    public abstract void runTask();
}
